﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using VIT.DataHelper.DataAccess;

namespace VIT.Business
{
    public class Supplier 
    {
        SQLSupport sqlSupport = new SQLSupport();

        #region Entities
        public int Id { get; set; }
        public string Name { get; set; }
        public string Image { get; set; }
        public string Description { get; set; }
        public int Orders { get; set; }
        public bool Published { get; set; }
        #endregion

        #region Constructor
        public Supplier(int id = 0,
                        string name = "",
                        string description = "",
                        string image = "")
        {
            this.Id = id;
            this.Name = name;
            this.Image = image;
            this.Description = description;
        }
        #endregion

        #region Edit
        public void GetSingle()
        {
            string strQuery = "select * from Product_Suppliers where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            DataTable dt = sqlSupport.ExecuteDataset(false, strQuery, Id).Tables[0];

            if (dt.Rows.Count > 0)
            {
                this.Name = dt.Rows[0]["Name"].ToString();
                this.Image = dt.Rows[0]["Image"].ToString();
                this.Description = dt.Rows[0]["Description"].ToString();
                if (!string.IsNullOrEmpty(dt.Rows[0]["Orders"].ToString()))
                    this.Orders = int.Parse(dt.Rows[0]["Orders"].ToString());
                if (!string.IsNullOrEmpty(dt.Rows[0]["Published"].ToString()))
                    this.Published = (bool)dt.Rows[0]["Published"]; 
            }
            else this.Id = 0;
        }

        public bool Exist()
        {
            string strQuery = "select Id from Product_Suppliers where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            return sqlSupport.ExecuteScalar(false, strQuery, Id) != null;
        }
        #endregion

    }
}
